﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace Idlesoft.Utils
{
	public static class RegexExtensions
	{
		#region ismatch
		public static bool IsMatch(this string text, string pattern)
		{
			return Regex.IsMatch(text, pattern);
		}
		#endregion ismatch

		#region match
		public static string Match(this string text, string pattern, params int[] groups)
		{
			var match = Regex.Match(text, pattern);
			return groups.IsNullOrEmpty() ? match.Value : groups.Fold((acc, group) => acc += match.Groups[group].Value, string.Empty);
		}
		#endregion match

		#region matches
		public static string[] Matches(this string text, string pattern, params int[] groups)
		{
			var matches = Regex.Matches(text, pattern).Cast<Match>();
			return !groups.IsNullOrEmpty() ? matches.Select(m => m.Value).ToArray() : matches.Select(match => groups.Fold((acc, group) => acc += match.Groups[group].Value, string.Empty)).ToArray();
		}
		#endregion matches

		#region replace
		public static string Replace(this string text, string pattern, string replacement)
		{
			return Regex.Replace(text, pattern, replacement);
		}
		#endregion replace
	}
}
